package com.krain.ddbb.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.krain.corelibrary.helper.imageloader.ImageLoader;
import com.krain.ddbb.R;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.constant.Constant;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.BaseEntity;
import com.krain.ddbb.entity.UserInfoBean;
import com.krain.ddbb.entity.UserIssueBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;

@EActivity(R.layout.activity_order_detail)
public class OrderDetailActivity extends BaseActivity {

    @ViewById(R.id.act_order_detail_desc)
    TextView tv_desc;

    @ViewById(R.id.act_order_detail_adress)
    TextView tv_adress;

    @ViewById(R.id.act_order_detail_contact)
    TextView tv_contact;

    @ViewById(R.id.act_order_detail_num)
    TextView tv_num;

    @ViewById(R.id.act_order_detail_price)
    TextView tv_price;

    @ViewById(R.id.act_order_detail_servetime)
    TextView tv_serveTime;

    @ViewById(R.id.act_order_detail_time)
    TextView tv_time;

    @ViewById(R.id.act_order_detail_phone)
    TextView tv_phone;

    @ViewById(R.id.act_orderdetail_tv_ordername)
    TextView tv_name;

    @ViewById(R.id.act_orderdetail_tv_order_quantity)
    TextView tv_server_num;

    @ViewById(R.id.score1)
    TextView score_limao;

    @ViewById(R.id.score2)
    TextView score_jishu;

    @ViewById(R.id.score3)
    TextView score_zhunshi;

    @ViewById(R.id.id_rel_robmsg)
    RelativeLayout rl_rob_person;

    @ViewById(R.id.act_confirm_select_person)
    Button btn_confirm;

    @ViewById(R.id.act_orderdetail_iv_icon)
    ImageView avater;

    @ViewById(R.id.ll_pics)
    LinearLayout ll_pics;

    @ViewById(R.id.act_again_order)
    Button btn_publish_order_again;//重新下单
    @Extra("orderBean")
    UserIssueBean orderBean;
    @Extra("id")
    String order_id;
    @Extra("type")
    int type;

    UserIssueBean userIssueBean;

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.order_detail);
    }

    @AfterViews
    void afterViews() {
        showProgressDialog();
    }


    @UiThread
    void setData(UserIssueBean bean) {
        tv_adress.setText(getString(R.string.serve_address, bean.getAddress()));
        tv_desc.setText(getString(R.string.order_need, bean.getDesc()));
        tv_num.setText(getString(R.string.order_number, bean.getOrder_num()));
        tv_price.setText(getString(R.string.serve_price, bean.getServer_price()));
        tv_serveTime.setText(getString(R.string.serve_time, bean.getServer_time()));
        tv_phone.setText(getString(R.string.order_phone, bean.getPhone()));
        tv_time.setText(getString(R.string.release_time, bean.getPublish_time()));
        tv_contact.setText(getString(R.string.order_contact, bean.getUsername()));
        if (ll_pics.getChildCount() > 0) ll_pics.removeAllViews();
        if (bean.getImage() != null && bean.getImage().size() > 0)
            for (final String str : bean.getImage()) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(200,200);
                ImageView image = new ImageView(this);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                image.setPadding(10, 10, 10, 10);
                image.setMaxHeight(200);
                ImageLoader.getInstance(mContext).loadImage(mContext, image, Constant.HOSTURL + str, R.mipmap.ic_launcher, 200, 200);
                image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext,PhotoViewActivity.class);
                        intent.putExtra("url",Constant.HOSTURL+str);
                        startActivity(intent);
                    }
                });
                ll_pics.addView(image,params);
            }
        initBottomLayout(bean);
    }


    /**
     * 根据技工和用户的身份初始化底部的布局
     */
    void initBottomLayout(UserIssueBean bean) {
        if (bean.getStatus() == UserIssueBean.STATUS_COMPLETE || bean.getStatus() == UserIssueBean.STATUS_CANCEL) {
            btn_confirm.setVisibility(View.GONE);
            btn_publish_order_again.setVisibility(View.GONE);
        }
        if (type == UserIssueBean.CURRENTSTATUS_WORKER) { //技工的身份
            btn_publish_order_again.setVisibility(View.GONE);
            if (bean.getStatus() == UserIssueBean.STATUS_WAIT_SERVICE) {
                btn_confirm.setVisibility(View.VISIBLE);
                btn_confirm.setText("确认服务完成");
            } else {
                if (bean.getStatus() == UserIssueBean.STATUS_COMPLETE || bean.getStatus() == UserIssueBean.STATUS_CANCEL
                        ||bean.getStatus() == UserIssueBean.STATUS_WAIT_CONFIRM){
                    tv_phone.setVisibility(View.GONE);
                    tv_adress.setVisibility(View.GONE);
                }
                btn_confirm.setVisibility(View.GONE);
            }
        } else {
            btn_publish_order_again.setVisibility(View.GONE);
            if (bean.getStatus() == UserIssueBean.STATUS_WAIT_PAY) {
                btn_confirm.setVisibility(View.VISIBLE);
                btn_confirm.setText("去付款");
            } else if (bean.getStatus() == UserIssueBean.STATUS_WAIT_SERVICE) {
                btn_confirm.setVisibility(View.GONE);
            } else if (bean.getStatus() == UserIssueBean.STATUS_WAIT_COMMENT) {
                btn_confirm.setText("去评论");
                btn_confirm.setVisibility(View.VISIBLE);
            } else if (bean.getStatus() == UserIssueBean.STATUS_WAIT_CONFIRM) {
                btn_confirm.setText("确认此人为我服务");
                btn_confirm.setVisibility(View.VISIBLE);
                btn_publish_order_again.setVisibility(View.VISIBLE);
            }else if (bean.getStatus() == UserIssueBean.STATUS_WAIT_ROB)
                btn_publish_order_again.setVisibility(View.VISIBLE);

        }
        if (bean.getServer_user() != null) {
            showServerMsg(bean);
        }

    }

    @Click(R.id.act_confirm_select_person)
    void confirmBtnClick() {

        if (userIssueBean.getStatus() == UserIssueBean.STATUS_WAIT_SERVICE) {
            showProgressDialog();
            confirmServerComplete();
        } else if (userIssueBean.getStatus() == UserIssueBean.STATUS_WAIT_COMMENT) {
            ServiceRemarkActivity.jumpTothisActivity(mContext, order_id, userIssueBean.getServer_user());
        } else if (userIssueBean.getStatus() == UserIssueBean.STATUS_WAIT_CONFIRM) {
            showProgressDialog();
            confirmServer(userIssueBean);
        } else if (userIssueBean.getStatus() == UserIssueBean.STATUS_WAIT_PAY) {
            goToPay(userIssueBean);
        }
    }

    @Background
    void confirmServerComplete() {
        BaseEntity entity = AppApi.getsInstance(mContext).confirmServerComplete(app.getmUserinfo().getAccess_token(), order_id);
        if (entity != null) {
            baseUtil.showSnackBar(mContext, "确认成功");
        }
        getDetail();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDetail();
    }

    @UiThread
    void hideConfirmButton() {
        btn_confirm.setVisibility(View.GONE);
    }


    class MyOnclickListener implements View.OnClickListener {

        UserIssueBean bean;

        public MyOnclickListener(UserIssueBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {

        }
    }


    /**
     * 去支付的方法
     *
     * @param bean
     */
    void goToPay(UserIssueBean bean) {
        Intent intent = new Intent(mContext, PayActivity_.class);
        intent.putExtra("bean", bean);
        startActivity(intent);
    }


    @Background
    void confirmServer(UserIssueBean bean) {
        BaseEntity entity = AppApi.getsInstance(getApplicationContext()).confirmServer(app.getmUserinfo().getAccess_token(), bean.getId());
        if (entity != null) {
            baseUtil.showSnackBar(mContext, "确认成功");
            afterViews();
        }
    }

    /**
     * 显示接单人的布局，接单之后显示
     *
     * @param bean
     */
    void showServerMsg(UserIssueBean bean) {
        if (type == UserIssueBean.CURRENTSTATUS_CLIENT)
            rl_rob_person.setVisibility(View.VISIBLE);
        tv_name.setText(bean.getServer_user().getUsername() + "  " + bean.getServer_user().getPhone());
        tv_server_num.setText(getString(R.string.order_quantity,bean.getServer_user().getOrder_num()));
        score_limao.setText(getString(R.string.politeness, bean.getServer_user().getPolite_score()));
        score_jishu.setText(getString(R.string.technology, bean.getServer_user().getTechnology_score()));
        score_zhunshi.setText(getString(R.string.punctuality, bean.getServer_user().getTime_score()));
        ImageLoader.getInstance(mContext).loadImage(mContext, avater, bean.getServer_user().getPicture(),R.mipmap.ic_launcher,200,200);
    }


    @Background
    void getDetail() {
        if (TextUtils.isEmpty(order_id))
            userIssueBean = AppApi.getsInstance(getApplicationContext()).getOrderDetail(app.getmUserinfo().getAccess_token(), orderBean.getId());
        else
            userIssueBean = AppApi.getsInstance(getApplicationContext()).getOrderDetail(app.getmUserinfo().getAccess_token(), Integer.parseInt(order_id));
        if (userIssueBean != null)
            setData(userIssueBean);
        dismissProgressDialog();
    }


    /**
     * 重新下单
     */
    @Click(R.id.act_again_order)
    void clickPublishAgain() {
        publishAgianAtBackgourd();
    }


    @Background
    void publishAgianAtBackgourd() {
        showProgressDialog();
        BaseEntity entity = AppApi.getsInstance(mContext).publishOrder(app.getmUserinfo().getAccess_token(), orderBean == null?Integer.parseInt(order_id):orderBean.getId());
        if (entity != null) {
            finish();
        }
        dismissProgressDialog();
    }

}
